package site.yunnong.atvris.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.api.Page;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.enumeration.MediaStatusEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.portal.util.SensitiveFilter;
import site.yunnong.atvris.dao.domain.Reply;
import site.yunnong.atvris.dao.domain.ReplyExample;
import site.yunnong.atvris.dao.domain.User;
import site.yunnong.atvris.dao.mapper.ReplyMapper;
import site.yunnong.atvris.common.dto.ReplyDeleteParam;
import site.yunnong.atvris.common.dto.ReplyParam;
import site.yunnong.atvris.common.dto.ReplyQueryParam;
import site.yunnong.atvris.common.dto.ReplyUpdateParam;
import site.yunnong.atvris.portal.service.ReplyService;
import site.yunnong.atvris.service.helper.OperatorHelper;
import site.yunnong.atvris.service.vo.ReplyVO;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 动态回复
 *
 * @author zhangjinghao
 * @email jhzhang9102@163.com
 * @date 2021-07-09 23:45:37
 */
@Service
public class ReplyServiceImpl implements ReplyService {
    
    @Resource
    private ReplyMapper replyMapper;

    @Override
    public Page<ReplyVO> queryPage(ReplyQueryParam param) {
        ReplyExample example = new ReplyExample();
        example.createCriteria()
                .andCidEqualTo(param.getCid())
                // 有效数据
                .andStatusNotEqualTo(MediaStatusEnum.NOT_PASSED.getValue());
        com.github.pagehelper.Page<Reply> replyPage = PageHelper.startPage(param.getPageNum(), param.getPageSize()).doSelectPage(() -> replyMapper.selectByExample(example));
        List<ReplyVO> results = Optional.of(replyPage.getResult())
                .orElse(Lists.newArrayList())
                .stream().map(this::buildReplyVO).collect(Collectors.toList());
        return new Page<>(replyPage.getPageNum(), replyPage.getPageSize(), replyPage.getTotal(), results);
    }

    private ReplyVO  buildReplyVO(Reply reply) {
        ReplyVO vo = new ReplyVO();
        BeanUtils.copyProperties(reply, vo);
        return vo;
    }

    @Override
    public ReplyVO save(ReplyParam param) {
        if (SensitiveFilter.containsSensitive(param.getContent())) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL, "输入内容包含敏感词");
        }
        Reply reply = new Reply();
        BeanUtils.copyProperties(param, reply);
        User user = OperatorHelper.getCurrentUser();
        reply.setUuid(user.getUuid());
        reply.setUserNickname(user.getNickname());
        reply.setStatus(MediaStatusEnum.ISSUE_REVIEW.getValue());
        reply.setCreateTime(LocalDateTime.now());
        replyMapper.insertSelective(reply);
        ReplyVO vo = new ReplyVO();
        BeanUtils.copyProperties(reply, vo);
        return vo;
    }

    @Override
    public int update(ReplyUpdateParam param) {
        return 0;
    }

    @Override
    public int removeByCid(Long cid) {
        ReplyExample example = new ReplyExample();
        example.createCriteria().andCidEqualTo(cid);
        return replyMapper.deleteByExample(example);
    }

    @Override
    public int removeMediaReply(ReplyDeleteParam param) {
        ReplyExample example = new ReplyExample();
        ReplyExample.Criteria criteria = example.createCriteria();
        if (param.getCid() != null) {
            criteria.andCidEqualTo(param.getCid());
        } else {
            criteria.andMediaIdEqualTo(param.getMediaId())
                    .andMediaTypeEqualTo(param.getMediaType());
        }
        return replyMapper.deleteByExample(example);
    }
}

